package com.acmedcare.framework.exchange.center.endpoint.exception;

import com.acmedcare.framework.exception.entity.EntityBody;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalDefaultExceptionHandler {

  @ExceptionHandler(Exception.class)
  public ResponseEntity<?> defaultExceptionHandler(HttpServletRequest request, Exception e) {

    if (e instanceof InvalidRequestParameterException) {

      return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(EntityBody.exception(e));
    }

    log.error("接口处理异常", e);
    return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(EntityBody.exception(e));
  }
}